﻿using AuthenticationMVC.Model;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthenticationMVC.Data
{
    public static class Seeder
    {
        public static void Seed(ApplicationDbContext db, bool roles = true, bool users = true, bool todos = true)
        {
            if (roles) SeedRoles(db);
            if (users) SeedUsers(db);
            if (todos) SeedToDos(db);
        }

        private static void SeedRoles(ApplicationDbContext db)
        {
            var store = new RoleStore<IdentityRole>(db);
            var manager = new RoleManager<IdentityRole>(store);

            //Creates a Role called "User"
            manager.Create(new IdentityRole()
            {
                Name = "User"
            });
            //Creates a Role called "Admin"
            manager.Create(new IdentityRole() { Name = "Admin" });
        }

        private static void SeedUsers(ApplicationDbContext db)
        {
            var store = new UserStore<ApplicationUser>(db);
            var manager = new UserManager<ApplicationUser>(store);

            if (!db.Users.Any(x => x.UserName == "test@test.com"))
            {
                ApplicationUser user = new ApplicationUser()
                {
                    UserName = "test@test.com",
                    Email = "test@test.com"
                };
                manager.Create(user, "123123");
                manager.AddToRole(user.Id, "User");
            }
            if (!db.Users.Any(x => x.UserName == "admin@test.com"))
            {
                ApplicationUser user = new ApplicationUser()
                {
                    UserName = "admin@test.com",
                    Email = "admin@test.com"
                };
                manager.Create(user, "123123");
                manager.AddToRole(user.Id, "Admin");
            }
        }

        private static void SeedToDos(ApplicationDbContext db)
        {
            //Finds the User IDs from the Users Table
            string testId = db.Users.FirstOrDefault(x => x.UserName == "test@test.com").Id;
            string adminId = db.Users.FirstOrDefault(x => x.UserName == "admin@test.com").Id;

            db.ToDos.AddOrUpdate(x => x.ToDoId,
              new ToDo() { ToDoId = 1, Description = "Learn Authentication", DateCreated = DateTime.Now, UserId = testId },
              new ToDo() { ToDoId = 2, Description = "Learn Authorization", DateCreated = DateTime.Now, UserId = testId, DateCompleted = DateTime.Now },
              new ToDo() { ToDoId = 3, Description = "Learn C#", DateCreated = DateTime.Now, UserId = testId },
              new ToDo() { ToDoId = 4, Description = "Learn Angular", DateCreated = DateTime.Now, UserId = adminId },
              new ToDo() { ToDoId = 5, Description = "Learn SQL", DateCreated = DateTime.Now, UserId = adminId, DateCompleted = DateTime.Now },
              new ToDo() { ToDoId = 6, Description = "Learn Entity Framework", DateCreated = DateTime.Now, UserId = adminId }
            );
        }
    }
}